package com.d404.server;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

/**
 * Servlet implementation class UploadAvar
 */

@MultipartConfig
public class UploadAvar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadAvar() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		String path = "E:/myimages";

		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String fileName = request.getParameter("data");
		Part part = request.getPart("file");
		System.out.println("part:" + path);
		int len = (int) part.getSize();
		System.out.println("len:" + len);

		String file = path + "/" + fileName;
		InputStream is = part.getInputStream();
		FileOutputStream fos = new FileOutputStream(file);
		System.out.println("准备接收");
		byte[] buf = new byte[len];
		is.read(buf);
		fos.write(buf);
		is.close();
		fos.close();
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
